<?php
/**
*  Create On 2014-12-03
*  Author yiwei
*  QQ:1006629314
**/
class trans extends Model{
	//检测班级是否存在
	public function testClass($code){
		$findClassSql = 'SELECT `id` FROM `xdf_class` WHERE `code` ="'.$code.'"';
		$class = $this->fetchRow($findClassSql);
		if($class['id']){
			return $class['id'];
		}else{
			return false;
		}
	}
	
	//检测学生是否存在
	public function testStudent($code){
		$findStuSql = 'SELECT `id` FROM `xdf_user` WHERE `code` ="'.$code.'"';
		$stu = $this->fetchRow($findStuSql);
		if($stu['id']){
			return $stu['id'];
		}else{
			return false;
		}
	}
	
	//检测教师是否存在
	public function testTeacher($code){
		$findTeaSql = 'SELECT `id` FROM `xdf_user` WHERE `code` ="'.$code.'"';
		$tea = $this->fetchRow($findTeaSql);
		if($tea['id']){
			return $tea['id'];
		}else{
			return false;
		}
	}
	
	//检测课时是否存在
	public function testPeriod($classId,$section){
		$findSection = 'SELECT `id` FROM `xdf_period` WHERE `class_id` = '.$classId.
		' AND `section` = '.$section;
		$check = $this->fetchRow($findSection);
		if($check['id']){
			return $check['id'];
		}else{
			return false;
		}
	}
	
	
	
	public function addClass($code,$name,$start,$end,$amount,$create_user_id){
		$sql = 'INSERT INTO `xdf_class` (`code`,`name`,`start_date`,`end_date`,
				`create_user_id`,`section_amount`,`create_time`) VALUES ("'.$code.'","'.$name.'",
				"'.$start.'","'.$end.'","'.$create_user_id.'","'.$amount.'",NOW())';		
		return $this->insert($sql);
	}
	
	public function addStudent($code,$name,$phone){
		$sql = 'INSERT INTO `xdf_user` (`code`,`name`,`phone`,`role_id`)
				VALUES ("'.$code.'","'.$name.'","'.$phone.'",7)';
		return $this->insert($sql);
	}
	
	public function addTeacher($code,$name,$phone){
		$sql = 'INSERT INTO `xdf_user` (`code`,`name`,`phone`,`role_id`)
				VALUES ("'.$code.'","'.$name.'",'.$phone.',6)';
		return $this->insert($sql);
	}
	
	public function bindClassStudent($stu_code,$class_code){
		$sql = 'INSERT INTO `xdf_class_student` (`class_id`,`student_id`)
				VALUES ('.$stu_code.','.$class_code.')';
		return $this->insert($sql);
	}
	
	public function addPeriod($course,$place,$section,$date,$start_time,$end_time,$class_code,$tea_code,$subject_id){
		$classId = $this->testClass($class_code);
		$teacherId = $this->testTeacher($tea_code);
		$periodId = $this->testPeriod($classId, $section);
		if($periodId){ 
			$sql = 'UPDATE `xdf_period` SET `course` = "'.$course.'",
					`place` = "'.$place.'",`date` = "'.$date.'",`start_time`
					= "'.$start_time.'",`end_time` = "'.$end_time.'",`class_id`
					= "'.$classId.'",`teacher_id` = "'.$teacherId.'"
					WHERE `id` = '.$periodId;
			return '更新'.$this->update($sql);
		}else{
			$sql = 'INSERT INTO `xdf_period` (`course`,`place`,`section`,`date`,
					`start_time`,`end_time`,`class_id`,`teacher_id`,`subject_id`) VALUES (
					"'.$course.'","'.$place.'","'.$section.'","'.$date.'",
					"'.$start_time.'","'.$end_time.'",'.$classId.','.$teacherId.','.$subject_id.')';
			//echo $sql;
			//exit;
			return '新增'.$this->insert($sql);
		}	
	}
}